home *** CD-ROM | disk | FTP | other *** search
/ Clickx 96 / Clickx 96.iso / software / tools / tool / xbmc-10.1.exe / addons / script.module.pil / lib / PIL / GdImageFile.py < prev    next >
Encoding:
Python Source  |  2009-04-06  |  2.1 KB  |  86 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # GD file handling
  6. #
  7. # History:
  8. # 1996-04-12 fl   Created
  9. #
  10. # Copyright (c) 1997 by Secret Labs AB.
  11. # Copyright (c) 1996 by Fredrik Lundh.
  12. #
  13. # See the README file for information on usage and redistribution.
  14. #
  15.  
  16.  
  17. # NOTE: This format cannot be automatically recognized, so the
  18. # class is not registered for use with Image.open().  To open a
  19. # gd file, use the GdImageFile.open() function instead.
  20.  
  21. # THE GD FORMAT IS NOT DESIGNED FOR DATA INTERCHANGE.  This
  22. # implementation is provided for convenience and demonstrational
  23. # purposes only.
  24.  
  25.  
  26. __version__ = "0.1"
  27.  
  28. import ImageFile, ImagePalette
  29.  
  30. def i16(c):
  31.     return ord(c[1]) + (ord(c[0])<<8)
  32.  
  33. ##
  34. # Image plugin for the GD uncompressed format.  Note that this format
  35. # is not supported by the standard <b>Image.open</b> function.  To use
  36. # this plugin, you have to import the <b>GdImageFile</b> module and
  37. # use the <b>GdImageFile.open</b> function.
  38.  
  39. class GdImageFile(ImageFile.ImageFile):
  40.  
  41.     format = "GD"
  42.     format_description = "GD uncompressed images"
  43.  
  44.     def _open(self):
  45.  
  46.         # Header
  47.         s = self.fp.read(775)
  48.  
  49.         self.mode = "L" # FIXME: "P"
  50.         self.size = i16(s[0:2]), i16(s[2:4])
  51.  
  52.         # transparency index
  53.         tindex = i16(s[5:7])
  54.         if tindex < 256:
  55.             self.info["transparent"] = tindex
  56.  
  57.         self.palette = ImagePalette.raw("RGB", s[7:])
  58.  
  59.         self.tile = [("raw", (0,0)+self.size, 775, ("L", 0, -1))]
  60.  
  61. ##
  62. # Load texture from a GD image file.
  63. #
  64. # @param filename GD file name, or an opened file handle.
  65. # @param mode Optional mode.  In this version, if the mode argument
  66. #     is given, it must be "r".
  67. # @return An image instance.
  68. # @exception IOError If the image could not be read.
  69.  
  70. def open(fp, mode = "r"):
  71.  
  72.     if mode != "r":
  73.         raise ValueError("bad mode")
  74.  
  75.     if type(fp) == type(""):
  76.         import __builtin__
  77.         filename = fp
  78.         fp = __builtin__.open(fp, "rb")
  79.     else:
  80.         filename = ""
  81.  
  82.     try:
  83.         return GdImageFile(fp, filename)
  84.     except SyntaxError:
  85.         raise IOError("cannot identify this image file")
  86.